import {
  TASK_LIST,
  TASK_SEARCH,
  SEARCH_TAG,
  TASK_BANNERS,
  TASK_CONTENT,
  BLANK_TASK
} from "../constants/task";
import { openGetJSON, getJSON, postJSON } from "../service/request";
import Taro from "@tarojs/taro";

/**
 * 首页banner
 */
export const getBanner = async () => {
  const { data } = await openGetJSON({ module: "TaskService.banner" });

  if (data && data.state === "000") {
    return {
      type: TASK_BANNERS,
      banners: data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {
      type: TASK_BANNERS
    };
  }
};

/**
 * 首页任务列表
 * @param {page:页数,type:结算类型} params
 */
export const getTaskList = async params => {
  const { data } = await openGetJSON({ ...params, module: "TaskService.list" });
  if (data && data.state === "000") {
    return dispatch => {
      dispatch({ type: BLANK_TASK });
      dispatch({ type: TASK_LIST, list: data.data, page: params.page });
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {
      type: TASK_LIST
    };
  }
};

/**
 * 任务详情
 * @param {id:任务id} params
 */
export const getTaskInfo = async params => {
  const { data } = await getJSON({ ...params, module: "TaskService.info" });
  if (data && data.state === "000") {
    return data;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {};
  }
};

/**
 * 任务内容详情
 * @param {id:任务id,puser:分享人id} params
 */
export const getTaskContent = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.content"
  });
  if (data && data.state === "000") {
    return {
      type: TASK_CONTENT,
      taskContent: data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {};
  }
};

export const getShareTaskContent = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.content"
  });
  if (data && data.state === "000") {
    return data;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {};
  }
};

/**
 * 推荐标签
 */
export const getSearchTags = async () => {
  const { data } = await openGetJSON({ module: "TaskService.searchTag" });
  if (data && data.state === "000") {
    return {
      type: SEARCH_TAG,
      tags: data.data
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {
      type: SEARCH_TAG
    };
  }
};

/**
 * 搜索
 * @param {key:关键词} params
 */
export const searchTaskForTag = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.search"
  });
  if (data && data.state === "000") {
    return {
      type: TASK_SEARCH,
      list: data.data,
      page: params.page
    };
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {
      type: TASK_SEARCH
    };
  }
};

/**
 * 分享回调
 * @param {id:任务id,puser:分享人} params
 */
export const shareReturn = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.shareReturn"
  });
  if (data && data.state === "000") {
    return data.data;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return false;
  }
};

/**
 * 阅读回调
 * @param {id:任务id,puser:分享人} params
 */
export const readReturn = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.readReturn"
  });
  if (data && data.state === "000") {
    return data.data;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return {};
  }
};

/**
 * 分享页面用户提交表单
 * @param {id:任务id,puser:分享人,name:姓名,tel:手机号码,code:验证码} params
 */
export const addOrder = async params => {
  const { data } = await openGetJSON({
    ...params,
    module: "TaskService.addOrder"
  });
  if (data && data.state === "000") {
    return data;
  } else {
    Taro.showToast({
      title: data.msg,
      icon: "none"
    });
    return false;
  }
};
